/*
 * 软件版权: 恒生电子股份有限公司
 * 修改记录:
 * 修改日期     修改人员  修改说明
 * ========    =======  ============================================
 * 2021/3/30  lisy31662  新增
 * ========    =======  ============================================
 */
package com.sansi.pencilbook.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.Enum.RelationEnum;
import com.sansi.pencilbook.entity.Book;
import com.sansi.pencilbook.entity.UserBook;
import com.sansi.pencilbook.entity.UserBookRelation;
import com.sansi.pencilbook.mapper.UserBookRelationMapper;
import com.sansi.pencilbook.service.UserBookRelationServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * 功能说明:
 *
 * @author lisy31662
 */
@Service
public class UserBookRelationServerImpl implements UserBookRelationServer {

  @Autowired
  private UserBookRelationMapper userBookRelationMapper;

  @Override
  public Result getUserBookRelationAll() {
    try{
      List<UserBookRelation> list = userBookRelationMapper.findAll();
      return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
          Result.ofFailed("查询为空");
    } catch (SQLException throwables) {
      throwables.printStackTrace();
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result getUserBookRelationByUser(int userId) {
    try{
      List<UserBookRelation> list = userBookRelationMapper.findByUser(userId);
      return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
          Result.ofFailed("查询为空");
    } catch (SQLException throwables) {
      throwables.printStackTrace();
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result getUserBookRelationByBook(String isbn) {
    try{
      List<UserBookRelation> list = userBookRelationMapper.findByBook(isbn);
      return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
          Result.ofFailed("查询为空");
    } catch (SQLException throwables) {
      throwables.printStackTrace();
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result getUserBookRelationByRelation(RelationEnum relationEnum) {
    try{
      List<UserBookRelation> list = userBookRelationMapper.findByRelation(relationEnum.getRelation());
      return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
          Result.ofFailed("查询为空");
    } catch (SQLException throwables) {
      throwables.printStackTrace();
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result getUserBookRelationByUserRelation(int userId, RelationEnum relationEnum) {
    try{
      List<UserBookRelation> list = userBookRelationMapper.findByUserRelation(userId, relationEnum.getRelation());
      return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
          Result.ofFailed("查询为空");
    } catch (SQLException throwables) {
      throwables.printStackTrace();
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result getUserBookRelationByUserRelation(int userId, RelationEnum relationEnum, int pageNum, int pageSize) {
    Page<UserBookRelation> page = PageHelper.startPage(pageNum, pageSize);
    try {
      userBookRelationMapper.findByUserRelation(userId, relationEnum.getRelation());
      return page.getTotal()>0 ? Result.ofSuccess("查询成功", page.size(), page.getResult()) :
          Result.ofFailed("查询结果为空");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      return Result.ofFailed("查询失败");
    }
  }

  @Override
  public Result getUserBookRelationByUserBook(int userId, String isbn) {
    try{
      List<UserBookRelation> list = userBookRelationMapper.findByUserBook(userId, isbn);
      return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
          Result.ofFailed("查询为空");
    } catch (SQLException throwables) {
      throwables.printStackTrace();
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result getUserBookRelationByUserBookRelation(int userId, String isbn, RelationEnum relationEnum) {
    try{
      List<UserBookRelation> list = userBookRelationMapper.findByUserBookRelation(userId, isbn, relationEnum.getRelation());
      return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
          Result.ofFailed("查询为空");
    } catch (SQLException throwables) {
      throwables.printStackTrace();
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result addUserBookRelation(UserBookRelation userBookRelation) {
    try{
      int num = userBookRelationMapper.saveUserBook(userBookRelation);
      return num>0 ? Result.ofSuccess("插入成功,插入关系为：" + userBookRelation.getRelation(), 1) :
          Result.ofFailed("插入失败");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      if (throwables.getMessage().contains("PRIMARY"))
        return Result.ofFailed("该书籍已经加入" + userBookRelation.getRelation());
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result modifyUserBookRelation(UserBookRelation userBookRelation) {
    try{
      int num = userBookRelationMapper.updateUserBook(userBookRelation);
      return num>0 ? Result.ofSuccess("插入成功,插入关系为：" + userBookRelation.getRelation(), 1) :
          Result.ofFailed("插入失败");
    } catch (Exception throwables) {
      System.out.println(throwables.getMessage());
      if (throwables.getMessage().contains("PRIMARY"))
        return Result.ofFailed("该书籍已经加入" + userBookRelation.getRelation());
      return Result.ofFailed(throwables.getMessage());
    }
  }

  @Override
  public Result deleteUserBookRelation(UserBookRelation userBookRelation) {
    try{
      int num = userBookRelationMapper.deleteUserBook(userBookRelation);
      return num>0 ? Result.ofSuccess("删除成功", 1) :
          Result.ofFailed("删除失败");
    } catch (SQLException throwables) {
      throwables.printStackTrace();
      return Result.ofFailed(throwables.getMessage());
    }
  }
}
